define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'content/content_banner/index' + location.search,
                    add_url: 'content/content_banner/add',
                    edit_url: 'content/content_banner/edit',
                    del_url: 'content/content_banner/del',
                    multi_url: 'content/content_banner/multi',
                    import_url: 'content/content_banner/import',
                    table: 'content_banner',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                columns: [
                    [
                        {checkbox: true},
                        // {field: 'id', title: __('Id')},
                        {field: 'image', title: __('Image'), events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'type', title: __('Type'), searchList: {"1":__('Type 1'),"2":__('Type 2'),"3":__('Type 3')}, formatter: Table.api.formatter.normal},
                        // {field: 'gymnasium_id', title: __('Gymnasium_id')},
                        // {field: 'coach_id', title: __('Coach_id')},
                        // {field: 'content_id', title: __('Content_id')},
                        {field: 'weigh', title: __('Weigh')},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'gymnasium.name', title: __('Gymnasium.name')},
                        // {field: 'coach.name', title: __('Coach.name')},
                        // {field: 'contentpage.title', title: __('Contentpage.title')},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            var gymnasium = $("#gymnasium_id");
            var coach = $("#coach_id");
            var content = $("#content_id");

            $('#c-type').on('change',function (){
                var val = Number($(this).val());
                if(val === 1){
                    $('form[role=form]').validator("setField", 'row[gymnasium_id]',"required,range(0~),integer(+)");
                    gymnasium.show();
                }else {
                    $('form[role=form]').validator("setField", 'row[gymnasium_id]',null);
                    gymnasium.hide();
                }
                if(val === 2){
                    $('form[role=form]').validator("setField", 'row[coach_id]',"required,range(0~),integer(+)");
                    coach.show();
                }else {
                    $('form[role=form]').validator("setField", 'row[coach_id]',null);
                    coach.hide();
                }
                if(val === 3){
                    $('form[role=form]').validator("setField", 'row[content_id]',"required,range(0~),integer(+)");
                    content.show();
                }else {
                    $('form[role=form]').validator("setField", 'row[content_id]',null);
                    content.hide();
                }
            });
            Controller.api.bindevent();
        },
        edit: function () {
            var gymnasium = $("#gymnasium_id");
            var coach = $("#coach_id");
            var content = $("#content_id");

            $('#c-type').on('change',function (){
                var val = Number($(this).val());
                if(val === 1){
                    $('form[role=form]').validator("setField", 'row[gymnasium_id]',"required,range(0~),integer(+)");
                    gymnasium.show();
                }else {
                    $('form[role=form]').validator("setField", 'row[gymnasium_id]',null);
                    gymnasium.hide();
                }
                if(val === 2){
                    $('form[role=form]').validator("setField", 'row[coach_id]',"required,range(0~),integer(+)");
                    coach.show();
                }else {
                    $('form[role=form]').validator("setField", 'row[coach_id]',null);
                    coach.hide();
                }
                if(val === 3){
                    $('form[role=form]').validator("setField", 'row[content_id]',"required,range(0~),integer(+)");
                    content.show();
                }else {
                    $('form[role=form]').validator("setField", 'row[content_id]',null);
                    content.hide();
                }
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});